Testing Interceptors

Implement ConfigurationProvider. This mocks your XWork configuration (xwork.xml). You can either have one configuration per interceptor under test or one for your whole suite.

MockConfigurationProvider.java
public class MockConfigurationProvider implements ConfigurationProvider {
 
  private static final String DEFAULT_PACKAGE = "defaultPackage";
  /** */
  public static final String TEST = "test";
 
  /* (non-Javadoc)
   * @see com.opensymphony.xwork.config.ConfigurationProvider#destroy()
   */
  public void destroy() {
    // do nothing
  }
 
  /* (non-Javadoc)
   * @see com.opensymphony.xwork.config.ConfigurationProvider#init(com.opensymphony.xwork.config.Configuration)
   */
  public void init(Configuration configurationManager) {
    PackageConfig defaultPackageContext = new   PackageConfig(DEFAULT_PACKAGE);
 
    List<InterceptorMapping> interceptors = new ArrayList<InterceptorMapping>();
    interceptors.add(new InterceptorMapping("test", new MyTestInterceptor()));
    ActionConfig actionConfig = new ActionConfig(null, MyAction.class, null, null, interceptors);
    actionConfig.setPackageName(DEFAULT_PACKAGE);
    defaultPackageContext.addActionConfig(TEST, actionConfig);
 
    configurationManager.addPackageConfig(DEFAULT_PACKAGE, defaultPackageContext);
  }
 
  /* (non-Javadoc)
   * @see com.opensymphony.xwork.config.ConfigurationProvider#needsReload()
   */
  public boolean needsReload() {
    return false;
  }
}

Then you can simply request the actions for testing from ActionProxyFactory, and they will have the correct interceptors set. The only other requirement is loading the mock configuration in set up

MyInterceptorUnitTest.java
public class MyInterceptorUnitTest extends TestCase {
 
  /* (non-Javadoc)
   * @see com.opensymphony.xwork.XWorkTestCase#setUp()
   */
  @Override
  protected void setUp() throws Exception {
    ConfigurationManager.clearConfigurationProviders();
    ConfigurationManager.addConfigurationProvider(new MockConfigurationProvider());
    ConfigurationManager.getConfiguration().reload();
  }
 
  /**
   * @throws Exception
   */
  public void testShouldOnlyAllowContinuationWhenInterceptorAllows() throws Exception {
    Map<String, Map><String, String>> extraContext = new HashMap<String, Map><String, String>>();
    extraContext.put(ActionContext.PARAMETERS, new HashMap<String, String>());
 
    ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy("", MockConfigurationProvider.TEST, extraContext);
    assertEquals("myredirect.result", proxy.execute());
  }
}

Existing Test Suites

Check out the test suites in XWork/WebWork. These are pretty comprehensive and provide a good starting point. For example, this is how the ParametersInterceptor is tested:

public void testDoesNotAllowMethodInvocations() {
  Map params = new HashMap();
  params.put("@java.lang.System@exit(1).dummy", "dumb value");

  HashMap extraContext = new HashMap();
  extraContext.put(ActionContext.PARAMETERS, params);

  try {
    ActionProxy proxy = ActionProxyFactory.getFactory().
      createActionProxy("", MockConfigurationProvider.MODEL_DRIVEN_PARAM_TEST, extraContext);
    assertEquals(Action.SUCCESS, proxy.execute());

    ModelDrivenAction action = (ModelDrivenAction) proxy.getAction();
    TestBean model = (TestBean) action.getModel();

    String property = System.getProperty("webwork.security.test");
    assertNull(property);
  } catch (Exception e) {
    e.printStackTrace();
    fail();
  }
}